A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

Case Statements Great But Could Be Excellent


The C switch/case/break system in my opinion sucks. It is verbose and you have to use switch case break case break case break case break. This is verbose, annoying and unnecessary. All that is not needed, especially in a language that is supposed to be terse. PHP, Java, and other languages have the same silly CASE statements with switch case break verbose line noise.

See also:
http://geeksnotnerds.com/node/110

Ugly annoying code:

switch (someVariable)
{
	case "1":
		doFunction1();
	break;
	
	case "2":
		doFunction2();
	break;

}

I think even the BASIC language case statement sucks (mentioned in that article).

The case statement in modern pascal is less verbose, clearer, and cleaner that both C and Basic.

Modern Pascal Case Statements

In modern pascal we have lovely case statements. They are much more elegant than in the Cee and PHP style languages which have verbose case/break keywords all over that bloat the source up with noise.

See the below case statement in modern pascal? There are no verbose CASE this, SWITCH this, BREAK that all over the code.

  case kind of  

      abc: 
          begin
            r:= 'blah';
            s:= 'hello';
            writeln(r, s);
          end;

      xyz: 
          begin
            y:= 5';
            writeln(y);
          end;
  else
      writeln('default case..');
  end;

Qompute Case Statements

The Qompute language has even better case statements than Pascal. The big "begin" and "end" don't get in the way in Qompute. However, Qompute language is still in development.

In Qompute:

  case kind of  
    2: b r2 =. Rec2;
         r2.s =. 'hello';
         Ln(r2.s);
       e;

    3: b r3 =. Rec3;
         r3.i =. 500;
         Ln(r3.i);
       e;  

    4: b r4 =. Rec4;
         r4.i =. 500;
         Ln(r4.i);
       e;  
  e;
If you try to do the above in Pascal you get:
  case kind of  
    2: begin r2:= Rec2; // doesn't line up
         r2.s:= 'hello';
         writeln(r2.s);
       end;

    3: begin r3:= Rec3; 
         r3.i:= 500;          // doesn't line up
         writeln(r3.i);
       end;  

    4: begin r4:= Rec4; // doesn't line up
         r4.i:= 500;
         writeln(r4.i);
       end;  // not same length as begin word
  end;
Two space indentation is the problem. So you could use more indentation..
    4: begin     r4:= Rec4;    // well it lines up now...
                 r4.i:= 500;
                 writeln(r4.i);
       end;  
  // but there is too much white space and fiddling !
but this requirement of constantly fiddling with code to try and get the perfect indentation is not worth the time.. so pascal programmers just put it on new lines. In qompute, it is more natural to just line it up immediately using two space indentation, either way.

So in pascal we are forced to split up code into several new lines, if we want the two space indentation preserved.

    k4: 
      begin 
        r4:= Rec4; 
        r4.i:= 500;
        writeln(r4.i);
      end;  

This makes algorithms span further down the screen and can make functions cluttered with more noise than they have to. But again, at least Pascal still has a better case statement than cee or php. In fact, it's hard to believe a better case statement could exist than the Pascal one!

Case statements are great in Pascal, but in Qompute they are even better.

  case kind of  
    something1: 
    b r2 =. Rec2;  // lovely
      s =.'hello';
      outln(r2.s);
    e;
 
    something2: 
    b r3 =. Rec3;  
      r3.i =. 500;
      Ln(r3.i);
    e;  

    something3: 
    b r4 =. Rec4;  
      r4.i =. 500;
      Ln(r4.i);
    e;  
  e;
Alternatively:
  case kind of  
    something1: b       
      r2 =. Rec2;    
      r2.s =. 'hello';
      Ln(r2.s);
    e;

    something2: b       
      r3 =. Rec3;
      r3.i =. 500;
      Ln(r3.i);
    e;  

    something3: b       
      r4 =. Rec4;
      r4.i =. 500;
      Ln(r4.i);
    e;  
  e;
The clever design of qompute keeps a lot of wasted clutter out of the code so that the programmer can focus on the algorithm as much as possible without bulky reserved words and unneeded new lines getting in the way.

In the last example shown above, you could put the 'begin' keyword there in the Pascal language.. but it looks funny and does not fit in well since it is bulky and stands out.

    something3: begin       
      writeln(r4.i);
      writeln('...');
   end;

    something3: begin       
                   writeln(r4.i);
                   writeln('...');
   end;

Begin is really bulky there, or there is too much white space needed to make it work with the indentation. In Qompute, the 'b' is so small that it is unobtrusive in these places.

Especially with syntax highlighting, one can find the start and end of a block easily with bold 'b' and 'e'.

If '{' and '}' are used they RUIN THE POETRY, and can be confused with '(' and ')' brackets on high resolution monitors. They also require shift-key on standard querty keyboards.. whereas 'b' and 'e' are fast to type on the keyboard with no shift key requirement.

Less Line Noise

The goal of Qompute isn't to become perl.. rather the goal to utilize the concepts of a clear language without the disadvantages of bulky verbosity either.

Once again.. the case statement in pascal is great already! Even Cee programmers admit that there is something wrong with the switch statements in Cee. However, in Qompute, the case statement is excellent or even greater than Pascal, and much better than in Cee.

It could be a matter of taste.. but sometimes if we study why certain syntaxes allow code to line up on the screen easier and neater, it becomes more obvious that it's more than just about taste and style. There is actually some thought put behind languages to make them easy to use. Not just easy on the eyes; the "b" and "e" are really easy to type on USA style keyboards too.. while curly parenthesis are not.

And no, please don't bring up 8 space tabs and python. Qompute author would rather source code read like poetry than big jumping chunks of whitespace ;)

Aesthetics, Poems, And Blocks

Of course, there can be aesthetics flamewars and opinions, and taste wars.. but any poet will tell you poems are more beautiful than morse code or modem noise. Qompute reminds me of terse poetry, whereas C/C++ reminds me of modem line noise. No offense.

Qompute also reads a bit like assembly code blocks do. To some, assembly code blocks are like poems, depending who you ask. I think assembly hackers will love the Qompute language too.

About
This site is about programming and other things.
_ _ _